package edu.zust.zw.shop.controller;

import java.io.File;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import edu.zust.zw.shop.service.PicService;
import edu.zust.zw.shop.util.Result;

@RequestMapping("/user")
@RestController
public class UserController extends BaseController {

    @Autowired
    private PicService picService;
    
    @Value("${uploadUrl}")
    private String uploadUrl;
     
    @PostMapping("uploadPic")
    public Result uploadPic(@RequestParam(value = "file", required = true)MultipartFile file) throws FileNotFoundException, IOException {
        String picUrl = picService.uploadPic(uploadUrl, file, getUserInfo());
        if (null != picUrl) {
            return Result.success("picUrl", picUrl);
        }
        
        return Result.error(Result.Code.PIC_UPLOAD_ERROR);
    }
}
